<!DOCTYPE html>
<html lang="en">

<head>
  <title></title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    #app {
      width: 600px;
      margin: 10px auto;
    }

    .tb {
      border-collapse: collapse;
      width: 100%;
    }

    .tb th {
      background-color: #0094ff;
      color: white;
    }

    .tb td,
    .tb th {
      padding: 5px;
      border: 1px solid black;
      text-align: center;
    }

    .add {
      padding: 5px;
      border: 1px solid black;
      margin-bottom: 10px;
    }
  </style>
</head>

<body>
  <!-- 
    1.添加一个可以自由添加新项目的功能
    2.对时间进行格式化处理
   -->
  <div id="app">
    <div>
      添加品牌：<input type="text" v-model="name"> <button @click="add">确定</button>
    </div>
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item,idx) in list" :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.time | formatTime}}</td>
        <td>
          <button @click="del(idx)">删除</button>
        </td>
      </tr>
      <tr v-if="list.length===0">
        <td colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="./moment.js"></script>
  <script src="./dayjs.min.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        name: '请输入',// 表示添加的品牌的名称
        list: [
          {
            id: 1,
            name: 'LV',
            time: '2020-06-24'
          },
          {
            id: 2,
            name: '范思哲',
            time: '2020-06-24'
          },
          {
            id: 3,
            name: '酷奇',
            time: '2020-06-24'
          }
        ]
      },
      methods: {
        del(idx) {
          this.list.splice(idx, 1)
        },
        // 添加新项目
        add() {
          // 注意保持数据格式的统一
          // id:如果list本身就是空的那id从1开始  如果list本身有值了  list最后一项的id加1之后成为新项目的id值
          let id
          if (this.list.length === 0) {
            id = 1
          } else {
            id = this.list[this.list.length - 1].id + 1
          }
          this.list.push({
            id: id,
            name: this.name,
            time: new Date()
          })
        }
      },
      filters: {
        formatTime(val) {
          return dayjs(val).format('YYYY-MM-DD')
        }
      }
    })
  </script>
</body>

</html>